const BASE_URL = "https://youyang.mdshuzhi.com";
// const BASE_URL = "http://springboot.nat300.top";
// https://api.leaf-tech.net

class Request {
    /**
     * Request请求方法
     * @param  {String} url    链接
     * @param  {Objece} params 参数
     * @param  {Boolean} isToken  是否携带token
     * @return {Promise}       包含抓取任务的Promise
     */
    getApi(url, params, isToken, method, istips) {
        if (isToken === undefined) isToken = true;
        if (istips === undefined) istips = true;
        if (method === undefined) method = 'GET';
        let token = wx.getStorageSync('logintoken') || '';
        const promise = new Promise((resolve, reject) => {
            //判断是否需要置入token
            if (isToken) params = Object.assign({
                'token': token
                // 3d955bd94ba449d9ab951c1fee4dfc2a
            }, params)
            wx.request({
                url: `${BASE_URL}${url}`,
                method: method,
                data: params, //置入token
                header: {
                    'Content-Type': 'application/json'
                },
                success: res => {
                    if (res.statusCode == 200) {
                        if (res.data.code == 0) {
                            resolve(res);
                        } else {
                            
                            if (istips) {
                                if (res.data.code == 101) {
                                    //token失效
                                    token = ""
                                    wx.setStorageSync('logintoken', '');
                                    resolve(res);
                                } else {
                                    wx.showToast({
                                        title: res.data.msg,
                                        icon: 'none',
                                        duration: 1200
                                    });
                                    reject();
                                }
                                
                            } else {
                                resolve(res);
                            }
                        }
                    } else {
                        wx.showToast({
                            title: '[' + res.code + '] 服务器出错,请重试',
                            icon: 'none',
                            duration: 1200
                        });
                        reject(res);
                    }

                },
                fail: err => {
                    wx.showToast({
                        title: '网络错误',
                        icon: 'none',
                        duration: 1200
                    });
                    reject(res);
                },
                complete: res => {

                }
            });
        });

        // if (token == '' && isToken) {
        //     return new Promise((resolve, reject) => {
        //         wx.showToast({
        //             title: '状态失效，请关闭小程序后，重新打开',
        //             icon: 'none'
        //         });

        //     });
        // } else {
        //     return promise;
        // }
        return promise
    }

}

let request = new Request();

module.exports = {
    get: request.getApi,
    post: request.getApi,
    url: BASE_URL
}